/*
 * 作者: 侯晓宇
 * 时间: 2017-01-11
 * 说明：
 * Copyright (c) 2016-2017 中研沃兮
 */
define(function(require) {

	require("jquery");
	var appInfo = require('app/AppInfo');
	var c = require('app/utils/CommonUtils');
	var hc = require('app/utils/HttpClientUtils');
	var tip = require('app/utils/TipUtils');
	var UserInfoUtils = require('app/utils/UserInfoUtils');
	var EchoLazyUtils = require('app/utils/EchoLazyUtils');
	var CacheUtils = require('app/utils/CacheUtils');
	var HashMap = require('app/utils/HashMapUtils');
	var APIUtils = require('app/utils/APIUtils');

	EchoLazyUtils.load();

	var action = {};

	var win = window;

	//catalogUuid: cataloguuid,
	//showBanner: showBanner
	win.param = api.pageParam;
	win.refreshtime = new Date().getTime();
	win.pos = 0;
	win.roomContainer = $("#rooms-div");
	win.class_empty_placeholder = "empty-placeholder";
	win.roomMap = new HashMap();
	action.fetchInHeaderMode = function() {

		var that = this;
		$("#rooms-div").html("");
		win.refreshtime = new Date().getTime();
		var url = appInfo.serverbase + "/wjt/catalog_rooms";
		var body = {
			cataloguuid: param.catalogUuid,
			refreshtype: "head", //all, head, foot
			timestamp: win.refreshtime,
			pos: win.pos,
			size: 15
		};


		hc.post(url, body, function(resp) {
			if (resp.status != "200") {
				api.refreshHeaderLoadDone();
				return;
			}
			api.refreshHeaderLoadDone();
			var data = resp.result;
			win.pos = resp.result.pos;
			if (data.rooms.length == 0) {
				action.renderEmptyRoom();
			} else {
				action.renderRooms(data);
			}

		});

	}

	action.fetchInFooterMode = function() {

		var that = this;
		var url = appInfo.serverbase + "/wjt/catalog_rooms";

		var body = {
			cataloguuid: param.catalogUuid,
			refreshtype: "footer",  //all, head, footer
			timestamp: win.refreshtime,
			pos: win.pos,
			size: 10
		};

		hc.post(url, body, function(resp){

			if(resp.status != "200"){
				return ;
			}
			var rooms = resp.result.rooms;
			win.pos = resp.result.pos;

			if(rooms.length == 0){
				api.toast({msg:'下面没有了'});
			}else{
				action.renderRooms(resp.result);
			}

		});

	}
	action.renderRooms = function(data) {

		var rooms = data.rooms;
		var divs = win.roomContainer = $("#rooms-div");
		var default_live_cover_url =  "../image/room_cover_default.png";
		var default_user_avatar_url =  "../image/user_avatar_default.jpg";
		$("." + win.class_empty_placeholder).remove();

		rooms.forEach(function(item) {
			c.debug("room",item);
			win.roomMap.put(item.uuid,item);
			//头像是否使用了缓存，如果头像地址为null，则用app中的默认图片，不使用缓存
			var avatar_use_cache = false;
			var cache_user_avatar_url = "";
			//封面是否使用了缓存，如果封面地址为null，则用app中的默认图片，不使用缓存
			var cover_use_cache = false;
			var cache_user_cover_url = "";

			var user_avatar_url = item.thumburl;
			if (c.isNullStr(user_avatar_url)) {
				user_avatar_url =default_user_avatar_url;
			} else {
				user_avatar_url = c.handleUrl(user_avatar_url);
				//网络地址，缓存图片
				cache_user_avatar_url = CacheUtils.imageCache(user_avatar_url);

				avatar_use_cache = true;
			}




			var live_cover_url = item.livecoverurl;
			if (c.isNullStr(live_cover_url) || live_cover_url == "null") {
				live_cover_url = default_live_cover_url;
			} else {
				live_cover_url = c.handleUrl(live_cover_url);
				//网络地址，缓存图片
				cache_user_cover_url = CacheUtils.imageCache(live_cover_url);
				cover_use_cache = true;
			}
			if(user_avatar_url == default_user_avatar_url){
				var user_avatar_div = $('<img src="'+default_user_avatar_url+'">');
			}else{
				if(cache_user_avatar_url == user_avatar_url){
					var user_avatar_div = $('<img data-echo="'+user_avatar_url+'" src="../image/user_avatar_default.jpg">');
				}else{
					var user_avatar_div = $('<img src="'+cache_user_avatar_url+'">');
				}

			}


			user_avatar_div.css({
				"border-radius": "50%",
				"width": "1.6rem",
				"height": "1.6rem"
			});


			var w = APIUtils.winW();
			var h = parseInt(w * 0.5625);

			if(live_cover_url == default_live_cover_url){
				var live_cover_div = $('<img src="'+default_live_cover_url+'" style="width:'+w+'px; height:'+h+'px">');
			}else{
				if(cache_user_cover_url == live_cover_url){
					var live_cover_div = $('<img data-echo="'+live_cover_url+'" src="../image/imgplaceholder.png" style="width:'+w+'px; height:'+h+'px">');
				}else{
					var live_cover_div = $('<img src="'+cache_user_cover_url+'" style="width:'+w+'px; height:'+h+'px">');
				}

			}



			if (item.roomchairs == null) {
				item.roomchairs = "匿名";
			}

			var live_type_div = '';
			if (item.liveType == "1") { // 视频 直播
				live_type_div　=
					'<div style="background: brown; border-radius:5px;position:absolute;font-size: 10px;padding: 5px;margin: 10px;color: white;left: 0;top:2rem;z-index:100;">'+
						'视频直播'+
					'</div>';
			} else if (item.liveType == "2") { // PPT+语音 直播
				live_type_div　 =
					'<div style="background: brown; border-radius:5px;position:absolute;font-size: 10px;padding: 5px;margin: 10px;color: white;left: 0;top:2rem;z-index:100;">' +
					'PPT+语音' +
					'</div>';
			} else if (item.liveType == "3") { // 外站其他直播或视频，但不现实PPT+语音字样, 视频
				live_type_div　 =
					'<div style="background: brown; border-radius:5px;position:absolute;font-size: 10px;padding: 5px;margin: 10px;color: white;left: 0;top:2rem;z-index:100;">' +
					'视频' +
					'</div>';
			}

			var status_div = '';
			var status_div_style = "position:absolute;font-size: 10px;padding:0.15rem 0.5rem 0.15rem 0.5rem;margin-top:2.5rem;color: white;border-radius: 0.1rem;right: 0;top:0;z-index:100;";
			if (item.type == "3") {
				status_div =
					'<div style="background: rgb(237,116,55);'+status_div_style+'">' +
					'回顾' +
					'</div>';
			} else if (item.type == "1") {
				var now = new Date().getTime();
				if (now < item.time) {
					status_div =
						'<div style="background: rgb(95,174,231);'+status_div_style+'">' +
						'预告' +
						'</div>';
				} else {

					if (now < item.endtime) {
						status_div =
							'<div style="background: cornflowerblue;'+status_div_style+'">' +
							'直播' +
							'</div>';
					} else {
						status_div =
							'<div style="background: rgb(237,116,55);'+status_div_style+'">' +
							'回顾' +
							'</div>';
					}
				}
			}

			var pwddiv = '<i class="aui-iconfont aui-icon-lock" style="color:#609700;font-weight:800"></i> &nbsp;';
			if(c.isNullStr(item.pwd) || item.pwd == false){
				pwddiv = "";
			}
			var div = $(
				'<div data-avatar_use_cache="'+avatar_use_cache+'" data-cover_use_cache="'+cover_use_cache+'" data-uuid="'+item.uuid+'" class="room-item aui-card-list" tapmode style="margin-bottom: 0.55rem;">' +
				'<div class="aui-card-list-header" style="height: 2rem;font-size:0.7rem;">'+
            	user_avatar_div.prop('outerHTML') +
            	'<div class="aui-ellipsis" style="position: absolute;left:2.8rem;line-height:2rem;height:2rem;top:0;">' +
//					item.roomchairs +
					pwddiv+ item.name +
				'</div>'+

        		'</div>'+
				'<div class="aui-card-list-content">' +
				live_cover_div.prop('outerHTML') +
				'</div>' +
				live_type_div +
				status_div +
				'<div style="color:#ffffff;font-size:0.6rem;background: rgba(0,0,0,0.5);position: absolute;bottom: 0px;width:100%;">' +
					'<span style="margin-left:0.75rem;line-height:2rem;">' + c.timeFormat(item.time) + '</span>' +
					'<span class="aui-pull-right" style="line-height:2rem;margin-right:0.75rem;">  <i class="aui-iconfont aui-icon-my"></i>&nbsp;&nbsp;&nbsp; '+item.attendeecount+'</span>'+
				'</div>'
			);
			divs.append(div);

		});

		$(".room-item").click(function(){

			var room = win.roomMap.get($(this).data("uuid"));
			room.avatar_use_cache =$(this).data("avatar_use_cache");
			room.cover_use_cache = $(this).data("cover_use_cache");
			c.debug("click room:",room);
			if(!UserInfoUtils.isLogined()){
			//未登陆则打开LoginWin.js
				if(api.systemType == "ios"){
					if(APIUtils.doesWXloginDisplay()){
						api.openWin({
									name: 'LoginAndRegistWin',
									url: './LoginAndRegistWin.html'
						});
						return;
					}else{
						api.openWin({
										name: 'RoomWin',
										url: './RoomWin.html',
										pageParam: room
						});
					}
				}else{
					api.openWin({
								name: 'LoginAndRegistWin',
								url: './LoginAndRegistWin.html'
					});
					return;
				}
			}else{
			//进入RoomWIn页面, 并传入参数room
				api.openWin({
		            name: 'RoomWin',
		            url: './RoomWin.html',
		            pageParam: room
	            });
			}


		});
		EchoLazyUtils.load();
		api.parseTapmode();


	}
	action.renderEmptyRoom = function() {
		var div = $("<div class=\"" + win.class_empty_placeholder + "\"></div>");
		div.css({
			width: "100%",
			"font-size": "0.8rem",
			color: "#666",
			"text-align": "center"
		});
		div.html("空空如也……");
		win.roomContainer.html("");
		win.roomContainer.append(div);
	}

	return action;
});
